package com.tubang.organization.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.tubang.common.constant.Constants;
import com.tubang.common.util.GsonQuick;
import com.tubang.common.util.MsgCode;
import com.tubang.common.util.ResultData;
import com.tubang.component.util.RequestUtil;
import com.tubang.organization.entity.MenuEntity;
import com.tubang.organization.entity.RoleEntity;
import com.tubang.organization.service.IMenuService;
import com.tubang.organization.service.IRoleService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author wsl
 * @since 2017-11-30
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
    private Logger logger = LoggerFactory.getLogger(getClass());



    @Resource
    private IMenuService menuService;

    /**
     * 获取菜单列表
     *
     * @return
     */
    @GetMapping(value = "getMenuList")
    public ResultData getMenuList(@RequestParam(required = false) String roleId,
                                  @RequestParam(required = false) String roleName,
                                  @RequestParam(required = false, defaultValue = "1") Integer page,
                                  @RequestParam(required = false, defaultValue = "10") Integer rows) {
        try {
            EntityWrapper<MenuEntity> entityWrapper = new EntityWrapper<MenuEntity>();
            if (StringUtils.isNotBlank(roleName)) {
                entityWrapper.eq("name", roleName);
            }

            if (StringUtils.isNotBlank(roleId)) {
                entityWrapper.eq("id", roleId);
            }
            entityWrapper.eq("enable", Constants.DEFAULT_VAULE_ONE);
            Page<MenuEntity> pageList = menuService.selectPage(new Page<MenuEntity>(page,rows), entityWrapper);
            int count = menuService.selectCount(entityWrapper);
            return ResultData.result(true).setData(pageList.getRecords()).setTotalCount(count);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("[MenuController]==> method[getMenuList],错误原因：[{}]", e.getMessage());
            return ResultData.result(false);
        }
    }


    /**
     * 获取菜单详情
     *
     * @return
     */
    @GetMapping(value = "getMenuInfo")
    public ResultData getMenuInfo(@RequestParam(required = false) String roleId) {
        try {
            MenuEntity menuEntity = menuService.selectById(roleId);
            return ResultData.result(true).setData(menuEntity);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("[MenuController]==> method[getMenuInfo],错误原因：[{}]", e.getMessage());
            return ResultData.result(false);
        }
    }


    /**
     * 菜单 添加 更新
     *
     * @return
     */
    @PostMapping(value = "insertOrUpdateMenuEntity")
    public ResultData insertOrUpdateMenuEntity(HttpServletRequest request) {
        try {
            String receivePostJson = RequestUtil.receivePostJson(request);
            MenuEntity menuEntity = GsonQuick.toObject(receivePostJson, MenuEntity.class);
            Timestamp now = new Timestamp(System.currentTimeMillis());
            String id = menuEntity.getId();
            if (StringUtils.isBlank(id)) {
                menuEntity.setCreateTime(now);
            }
            menuEntity.setUpdateTime(now);
            menuService.insertOrUpdate(menuEntity);
            return ResultData.result(true).setData(menuEntity);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("[MenuController]==> method[insertOrUpdateMenuEntity],错误原因：[{}]", e.getMessage());
            return ResultData.result(false);
        }
    }

    /**
     * 删除菜单
     *
     * @return
     */
    @RequestMapping(value = "deleteMenuEntity", method = RequestMethod.GET)
    public ResultData deleteMenuEntity(@RequestParam String roleId) {
        try {
            MenuEntity menuEntity = menuService.selectById(roleId);
            if (menuEntity == null) {
                return ResultData.result(false).setCode(MsgCode.ADDRESS_FAIL_IS_NOT_EXIST).setMsg(MsgCode.ADDRESS_FAIL_IS_NOT_EXIST_DESC);
            }
            // 更新状态
            menuEntity.setEnable(Boolean.FALSE);
            menuService.updateById(menuEntity);
            return ResultData.result(true);
        } catch (Exception e) {
            return ResultData.result(false).setMsg(MsgCode.DEFAULT_ERROR_MSG);
        }
    }
}
